# # coding: utf-8
# import numpy as np
# import math
# import matplotlib.pyplot as plt
# import matplotlib.cbook as cbook
#
# data=[-35,10,20,30,40,50,60,106]
# labels = ['Label']
# flierprops = {'marker':'o','markerfacecolor':'red','color':'black'}
#
# # 使用matplotlib的cbook模块中的boxplot_stats函数计算箱线图的统计信息
# # 包括最小值、第一四分位数、中位数、第三四分位数、最大值以及异常值等
# stats = cbook.boxplot_stats(data,labels=labels)
# print (stats)
# k=75.0
# q3=np.percentile(data,k,interpolation='linear')
# print (k,'%:',q3)
# index1=1+(len(data)-1)*k/100.0
# print ('posision:',index1)
#
# # 计算75%分位数的近似值
# i=int(math.floor(index1))
# fraction1=index1-i
# print (i,fraction1)
# myq3=data[i-1]+(data[i]-data[i-1])*fraction1
# print ('myQi=',myq3)
#
# #fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
# # fig, axes = plt.subplots()
# # axes.bxp(stats)
#
# # 绘制箱线图
# # 使用数据集绘制箱线图，notch=False表示不使用缺口表示置信区间，flierprops定义异常值的属性
# plt.grid(True, linestyle = "-.", color = "black", linewidth = "0.4")   # 设置网格线
# plt.boxplot(data,notch=False,flierprops=flierprops)
#
# plt.show()
import math

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from matplotlib.cbook import boxplot_stats

# 加载 Iris 数据集并取前五条数据
iris = load_iris()
data = np.c_[iris['data'], iris['target']][:5]
data = data[:, 0]  # 假设取第一个特征列

labels = ['Iris']
flierprops = {'marker':'o','markerfacecolor':'red','color':'black'}

# 使用 matplotlib 的 cbook 模块中的 boxplot_stats 函数计算箱线图的统计信息
stats = boxplot_stats(data, labels=labels)
print(stats)
k = 75.0
q3 = np.percentile(data, k, interpolation='linear')
print(k, '%:', q3)
index1 = 1+(len(data)-1)*k/100.0
print('posision:', index1)

# 计算 75%分位数的近似值
i = int(math.floor(index1))
fraction1 = index1 - i
print(i, fraction1)
myq3 = data[i-1]+(data[i]-data[i-1])*fraction1
print('myQi=', myq3)

plt.grid(True, linestyle = "-.", color = "black", linewidth = "0.4")   # 设置网格线
plt.boxplot(data,notch=False,flierprops=flierprops)

plt.show()